var Pollution = {};

Pollution.getPollutionClass = function(value, substance) {
	var classNames = [ "low-1", "low-2", "low-3", "moderate-1", "moderate-2",
			"moderate-3", "high-1", "high-2", "high-3", "very-high" ];
	var level = Pollution.getPollutionLevel(value, substance);
	if (level == -1)
		return "na";
	return classNames[level];
};
Pollution.getAirQualityBanding = function(aqi) {
	if (aqi >= 1 && aqi <= 3) {
		return 0;// low
	} else if (aqi >= 4 && aqi <= 6) {
		return 1;// moderate
	} else if (aqi >= 7 && aqi <= 9) {
		return 2;// high
	} else if (aqi == 10) {
		return 3;// very high
	}
};

Pollution.getPollutionLevel = function(value, substance) {
	var boundaries = [];
	switch (substance) {
	case "o3":
		boundaries = [ 0, 34, 67, 101, 121, 141, 161, 188, 214, 241 ];
		break;
	case "no2":
		boundaries = [ 0, 68, 135, 201, 268, 335, 401, 468, 535, 601 ];
		break;
	case "so2":
		boundaries = [ 0, 89, 178, 267, 355, 444, 533, 711, 888, 1065 ];
		break;
	case "pm2p5":
		boundaries = [ 0, 12, 24, 36, 42, 48, 54, 59, 65, 71 ];
		break;
	case "pm10":
		boundaries = [ 0, 17, 34, 51, 59, 67, 76, 84, 92, 101 ];
		break;
	default:
		return -1;
	}
	var i = 0;
	for (; i < boundaries.length; i++) {
		if (value < boundaries[i]) {
			break;
		}
	}
	return i - 1;
};

Pollution.getAQI = function(o) {
	var substance_list = [ "no2", "pm10", "so2", "o3", "pm2p5" ];
	var max = -1;
	maxS = "";
	for (var i = 0; i < substance_list.length; i++) {
		var level = Pollution.getPollutionLevel(o[substance_list[i]],
				substance_list[i]);
		if (level > max) {
			max = level;
			maxS = substance_list[i];
		}
	}
	return max;
};

Pollution.aqiColor = function(o, opacity) {
	opacity = opacity || 0.6;
	var colors = [ "rgba(156, 255, 156, " + opacity + ")",
			"rgba(49, 255, 0, " + opacity + ")",
			"rgba(49, 207, 0, " + opacity + ")",
			"rgba(255, 255, 0, " + opacity + ")",
			"rgba(255, 207, 0, " + opacity + ")",
			"rgba(255, 154, 0, " + opacity + ")",
			"rgba(255, 100, 100, " + opacity + ")",
			"rgba(255, 0, 0, " + opacity + ")",
			"rgba(153, 0, 0, " + opacity + ")",
			"rgba(206, 48, 255, " + opacity + ")" ];

	var substance_list = [ "no2", "pm10", "so2", "o3", "pm2p5" ];
	var max = -1;
	maxS = "";
	for (var i = 0; i < substance_list.length; i++) {
		var level = Pollution.getPollutionLevel(o[substance_list[i]],
				substance_list[i]);
		if (level > max) {
			max = level;
			maxS = substance_list[i];
		}
	}
	return colors[max];
};

Pollution.getAirQualityClass = function(o) {
	var classNames = [ "low-1", "low-2", "low-3", "moderate-1", "moderate-2",
			"moderate-3", "high-1", "high-2", "high-3", "very-high" ];

	return classNames[Pollution.getAQI(o)];
};

Pollution.getHealthMessage = function(airPollutionBanding, atRisk) {
	var messagesForGeneralPopulation = [
			"Enjoy your usual outdoor activities.",
			"Enjoy your usual outdoor activities.",
			"Anyone experiencing discomfort such as sore eyes, cough or sore throat should consider reducing activity, particularly outdoors.",
			"Reduce physical exertion, particularly outdoors, especially if you experience symptoms such as cough or sore throat." ];
	var footerMessagesForGeneralPopulation = "";
	var messagesForAtRiskIndividuals = [
			"Enjoy your usual outdoor activities.",
			"Adults and children with lung problems, and adults with heart problems, who experience symptoms, should consider reducing strenuous physical activity, particularly outdoors.",
			"Adults and children with lung problems, and adults with heart problems, should reduce strenuous physical exertion, particularly outdoors, and particularly if they experience symptoms. People with asthma may find they need to use their reliever inhaler more often. Older people should also reduce physical exertion.",
			"Adults and children with lung problems, adults with heart problems, and older people, should avoid strenuous physical activity. People with asthma may find they need to use their reliever inhaler more often." ];

	var footerMessagesForAtRiskIndividuals = "Adults and children with heart or lung problems are at greater risk of symptoms. Follow your doctor's usual advice about exercising and managing your condition. It is possible that very sensitive individuals may experience health effects even on Low air pollution days. Anyone experiencing symptoms should follow the guidance provided above.";

	if (atRisk) {
		return [ messagesForAtRiskIndividuals[airPollutionBanding],
				footerMessagesForAtRiskIndividuals ];
	} else {
		return [ messagesForGeneralPopulation[airPollutionBanding],
				footerMessagesForGeneralPopulation ];
	}
};
